package com.mask.oss.controller;


import com.mask.oss.service.OssService;
import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.util.RollResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/oss")
@CrossOrigin
@Api(tags = "oss对象存储")
public class OssController {

    @Autowired
    private OssService ossService;

    /**
     * oss上传讲师头像
     * @param file
     * @return
     */
    @PostMapping("/upload/avatar")
    @ApiOperation("讲师头像上传")
    public RollResponse uploadOssFile(MultipartFile file) {
        //获取上传文件  MultipartFile
        //返回上传到oss的路径
        String url = ossService.uploadFile(file,RollConstants.TEACHER_AVATAR);
        return RollResponse.result(
                ServiceEnums.OSS_UPLOAD_AVATAR.getCode(), RollConstants.SUCCESS,
                ServiceEnums.OSS_UPLOAD_AVATAR.getMessage(), url);
    }

    /**
     * oss上传课程封面图片
     * @param file
     * @return 地址
     */
    @PostMapping("/upload/course/cover")
    @ApiOperation("课程封面上传")
    public RollResponse uploadCourseCover(MultipartFile file){
        String url = ossService.uploadFile(file, RollConstants.COURSE_COVER);
        return RollResponse.result(
                ServiceEnums.OSS_UPLOAD_COURSE_COVER.getCode(), RollConstants.SUCCESS,
                ServiceEnums.OSS_UPLOAD_COURSE_COVER.getMessage(), url);
    }
}
